package com.mobilesales.android.activities.employees;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.mobilesales.android.R;
import com.mobilesales.android.activities.ListModelActivity;
import com.mobilesales.android.adapters.MessageAdapter;
import com.mobilesales.android.database.EmployeeManager;
import com.mobilesales.android.model.employees.Employee;
import com.mobilesales.android.model.employees.Message;
import com.mobilesales.android.util.Constants;
import com.mobilesales.android.util.FilterFieldWatcher;
import com.mobilesales.android.util.MessageBox;

public class Messages extends ListModelActivity<Message> {

	private List<Employee> employees;
	private List<Message> messages;
	private MessageAdapter messageAdapter;
	private FilterFieldWatcher filter;
	private EditText searchField;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.messages);
		searchField = (EditText) this.findViewById(R.id.messageFilterText);
		loadItems();
	}

	private void loadItems() {
		employees = getEmployees();
		if (employees == null) {
			MessageBox.show(this, getString(R.string.ErrorEmployeeList));
			finish();
		}
		messages = getItems();
		if (messages != null)
			this.setItemsSource(messages);
		else
			MessageBox.show(this, getString(R.string.ErrorMessageList));
	}

	@Override
	protected void onListItemClick(ListView listView, View view, int position,
			long id) {
		super.onListItemClick(listView, view, position, id);
		Message msg = (Message) this.messageAdapter.getItem(position);
		Intent intent = new Intent(this, MessageRead.class);
		intent.putExtra(Constants.MessageId, msg.Id);
		startActivityForResult(intent,0);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		this.loadItems();
	}	

	public void compose(View v) {
		Intent intent = new Intent(this, MessageSend.class);
		startActivity(intent);
	}

	@Override
	protected void setItemsSource(List<Message> source) {
		Collections.sort(source,new Comparator<Message>() {
			public int compare(Message m1, Message m2) {
				return m2.SendDate.compareTo(m1.SendDate);
			}
		});
		messageAdapter = new MessageAdapter(this, R.layout.message_list_item,
				source, employees);
		this.setListAdapter(messageAdapter);
		this.filter = new FilterFieldWatcher(this.messageAdapter);
		this.searchField.addTextChangedListener(this.filter);
	}

	private List<Employee> getEmployees() {
		EmployeeManager manager = new EmployeeManager(this);
		List<Employee> employees = manager.GetEmployeeList();
		manager.Close();
		return employees;
	}

	@Override
	protected List<Message> getItems() {
		EmployeeManager manager = new EmployeeManager(this);
		List<Message> messages = manager.GetMessageList();
		manager.Close();
		return messages;
	}

	public void refresh() {
		this.getHandler().post(new Runnable() {
			public void run() {
				loadItems();
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (this.filter != null)
			this.searchField.removeTextChangedListener(this.filter);
	}
}
